//
//  XMGTabBar.m
//  BuDeJie


#import "XMGTabBar.h"
#import "U9LeftMenuViewModel.h"
#import "U9CatagoryModel.h"
#import "U9TabBarButton.h"

@interface XMGTabBar ()

@property (nonatomic, strong) NSMutableArray *buttonArray;

@property (nonatomic, assign) UIEdgeInsets oldSafeAreaInsets;

@end

@implementation XMGTabBar

-(NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [NSMutableArray array];
        
    }
    return _buttonArray;
}

+ (instancetype)tabBarWithGid:(NSString *)GID{
    XMGTabBar *tabbar = [[self alloc] initWithGID:GID];
    return tabbar;
}

- (instancetype)initWithGID:(NSString *)GID{
    if (self = [super init]) {
        _oldSafeAreaInsets = UIEdgeInsetsZero;
        U9TabBarButton *hotbutton = [U9TabBarButton tabBarButton];
        [hotbutton  setTitle:@"热点" forState:UIControlStateNormal];
        [hotbutton setImage:[UIImage imageNamed:@"tabbar_hotPoint"] forState:UIControlStateNormal];
        [hotbutton setImage:[UIImage imageNamed:@"tabbar_hotPoint_selected"] forState:UIControlStateSelected];
        [hotbutton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:hotbutton];
        
        U9TabBarButton *matchbutton = [U9TabBarButton tabBarButton];
        [matchbutton  setTitle:@"赛事" forState:UIControlStateNormal];
        [matchbutton setImage:[UIImage imageNamed:@"tabbar_match"] forState:UIControlStateNormal];
        [matchbutton setImage:[UIImage imageNamed:@"tabbar_match_selected"] forState:UIControlStateSelected];
        [matchbutton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:matchbutton];
        
        U9TabBarButton *wodeButton = [U9TabBarButton tabBarButton];
        [wodeButton setTitle:@"我的" forState:UIControlStateNormal];
        [wodeButton setImage:[UIImage imageNamed:@"tabbar_wode"] forState:UIControlStateNormal];
        [wodeButton setImage:[UIImage imageNamed:@"tabbar_wode_selected"] forState:UIControlStateSelected];
        [wodeButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:wodeButton];
        
        U9TabBarButton *toolbutton = [U9TabBarButton tabBarButton];
        [toolbutton  setTitle:@"工具" forState:UIControlStateNormal];
        [toolbutton setImage:[UIImage imageNamed:@"ico_tool"] forState:UIControlStateNormal];
        [toolbutton setImage:[UIImage imageNamed:@"ico_tool_b"] forState:UIControlStateSelected];
        [toolbutton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttonArray addObject:toolbutton];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _oldSafeAreaInsets = UIEdgeInsetsZero;
    NSInteger gid = [[NSUserDefaults standardUserDefaults] integerForKey:@"gid"];
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for (CatagoryModel *model in delegate.leftMenuVM.categoryArray) {
        if (model.catagoryID == gid) {

            [self modifyTabBarItemWithType:model.tab];
             break;
        }
    }
}

- (void)modifyTabBarItemWithType:(CatagoryTabModel *)model{
    
    for (UIControl *tabBarButton in self.subviews) {
        [tabBarButton removeFromSuperview];
        
    }
    NSMutableArray *newArray = [NSMutableArray array];
    if (model.redian) {
        [newArray addObject:self.buttonArray[0]];
    }
    if (model.saishi) {
        [newArray addObject:self.buttonArray[1]];
    }
    if (model.wode) {
        [newArray addObject:self.buttonArray[2]];
    }
    if (model.gongju){
        [newArray addObject:self.buttonArray[3]];
    }
    
    CGFloat btnX = 0;
    CGFloat btnY = 0;
    
    CGFloat btnW = kScreenWidth / newArray.count;
    CGFloat btnH = self.frame.size.height;
    if (iPhoneXSeries) {
        btnH = self.frame.size.height -20;
    }
    
    for (int i = 0; i < newArray.count; i++) {
        U9TabBarButton *tabBarButton = newArray[i];
        if (i == 0 && self.previousClickedTabBarButton == nil) { // 最前面的tabBarButton
            self.previousClickedTabBarButton = tabBarButton;
            tabBarButton.selected = YES;
        }
        btnX = i * btnW;
        tabBarButton.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [self addSubview:tabBarButton];
    }
    
    
    
    
    
    
    
    // 监听点击
    // 短时间内连续点击按钮就会触发UIControlEventTouchDownRepeat事件
    //        [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
    //    [tabBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)buttonClick:(UIButton *)tabBarButton
{
    self.previousClickedTabBarButton.selected = NO;
    tabBarButton.selected = YES;
    //    if (self.previousClickedTabBarButton == tabBarButton) {
    // 告诉外界，tabBarButton被重复点击了
    //        [[NSNotificationCenter defaultCenter] postNotificationName:XMGTabBarButtonDidRepeatClickNotification object:nil];
    //    }
    if ([_clickdelegate respondsToSelector:@selector(onTabBarClick:)]) {
        [_clickdelegate onTabBarClick:tabBarButton];
    }
    
}

-(void)selectedFirstButton{
    UIButton *tabBarButton = [self.buttonArray objectAtIndex:0];
    self.previousClickedTabBarButton.selected = NO;
    tabBarButton.selected = YES;
    self.previousClickedTabBarButton = tabBarButton;
}

- (void)safeAreaInsetsDidChange {
    [super safeAreaInsetsDidChange];
    
    if (!UIEdgeInsetsEqualToEdgeInsets(_oldSafeAreaInsets, self.safeAreaInsets)) {
        [self invalidateIntrinsicContentSize];
        
        if (self.superview) {
            [self.superview setNeedsLayout];
            [self.superview layoutSubviews];
        }
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    size = [super sizeThatFits:size];
    
    if (@available(iOS 11.0, *)) {
        float bottomInset = self.safeAreaInsets.bottom;
        if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
            size.height += bottomInset;
        }
    }
    
    return size;
}


- (void)setFrame:(CGRect)frame {
    if (self.superview) {
        if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
            frame.origin.y = self.superview.frame.size.height - frame.size.height;
        }
    }
    [super setFrame:frame];
}


@end
